package com.fileshare.web.service.impl;

import com.fileshare.web.dao.UserRepository;
import com.fileshare.web.dto.Vo.UserVo;
import com.fileshare.web.entity.User;
import com.fileshare.web.service.EmailService;
import com.fileshare.web.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;
    @Resource
    private EmailService emailService;

    @Override
    public User queryThirdPartyUser(String userType, String thirdPartySeq) {
        return userRepository.findByTypeAndPartySeq(userType, thirdPartySeq);
    }

    @Override
    public void addUser(User user) {
        if (user.getEmail() != null) {
            emailService.sendWelcomeNotification(user);
        }
        userRepository.save(user);
    }

    @Override
    public String generateUserSeq() {
        return userRepository.generateSeq();
    }

    @Override
    public List<UserVo> getAll() {
        return userRepository.findAll().stream().map(UserVo::fromEntity).collect(Collectors.toList());
    }

    @Override
    public User getUserByUserName(String account) {
        return userRepository.findUserByUserName(account);
    }

    @Override
    public List<User> getUsers() {
        return userRepository.findAll();
    }

    @Override
    public UserVo getUserBySeq(String userSeq) {
        return UserVo.fromEntity(userRepository.findBySeq(userSeq));
    }
}
